package com.example.helloworld

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class PureLayout : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        // 创建主布局容器
        val mainLayout = createMainLayout()
        setContentView(mainLayout)

        // 设置窗口边距处理
        ViewCompat.setOnApplyWindowInsetsListener(mainLayout) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 获取UI元素引用
        val imageView = mainLayout.findViewById<ImageView>(R.id.imageview)
        val textView = mainLayout.findViewById<TextView>(R.id.textview)
        val button = mainLayout.findViewById<Button>(R.id.button)

        // 设置文本内容
        textView.text = getString(R.string.textview)

        // 设置按钮点击事件
        button.setOnClickListener {
            textView.text = getString(R.string.clicked)
        }
    }

    private fun createMainLayout(): LinearLayout {
        // 创建主LinearLayout容器
        val mainLayout = LinearLayout(this).apply {
            id = R.id.main
            orientation = LinearLayout.VERTICAL
            gravity = Gravity.CENTER
            setBackgroundColor(Color.parseColor("#E8F4FD")) // 淡蓝色背景
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        }

        // 创建ImageView
        val imageView = ImageView(this).apply {
            id = R.id.imageview
            setImageResource(R.drawable.flag)
            scaleType = ImageView.ScaleType.CENTER_INSIDE
            layoutParams = LinearLayout.LayoutParams(
                dpToPx(280),
                dpToPx(200)
            ).apply {
                bottomMargin = dpToPx(50)
            }
        }

        // 创建TextView
        val textView = TextView(this).apply {
            id = R.id.textview
            text = getString(R.string.textview)
            textSize = 36f
            setTextColor(Color.parseColor("#2C3E50"))
            typeface = Typeface.DEFAULT_BOLD
            gravity = Gravity.CENTER
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                bottomMargin = dpToPx(50)
            }
        }

        // 创建Button
        val button = Button(this).apply {
            id = R.id.button
            text = getString(R.string.button)
            textSize = 20f
            setTextColor(Color.WHITE)
            setBackgroundColor(Color.parseColor("#FF6B6B")) // 珊瑚红色
            layoutParams = LinearLayout.LayoutParams(
                dpToPx(220),
                dpToPx(60)
            )
            // 添加一些内边距让按钮更美观
            setPadding(dpToPx(20), dpToPx(15), dpToPx(20), dpToPx(15))
        }

        // 将子视图添加到主布局中
        mainLayout.addView(imageView)
        mainLayout.addView(textView)
        mainLayout.addView(button)

        return mainLayout
    }

    private fun dpToPx(dp: Int): Int {
        val density = resources.displayMetrics.density
        return (dp * density).toInt()
    }
}
